package com.bailiban.aigou.admin.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.reference;
import com.bailiban.aigou.admin.entity.BackstageUser;
import com.bailiban.aigou.admin.service.IAigouBackstageService;
import com.bailiban.aigou.admin.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@ParentPackage("struts-default")
@Scope("prototype")
/*@Namespace("/")*/
@Results({
	@Result(name = "error", location = "/WEB-INF/jsp/aigou/admin/login/login.jsp"),
	@Result(name = "input", location = "/WEB-INF/jsp/aigou/admin/login/login.jsp")
})
public class AigouBackstageAction extends ActionSupport {
	/**
	 * 类名：AigouBackstageAction
	 * 类型：@Controller层  action
	 * 功能描述：这里只做对后台登录页面的操作
	 * **/
	private static final long serialVersionUID = 1L;
	
	 @Action(value="",results={ @Result( name="success" ,location ="/WEB-INF/jsp/aigou/admin/login/login.jsp")})
	    public  String LoginAction(){
			System.out.println("后台aigou");
	    	   return SUCCESS;
			
		 }
	 @Resource
	 private IAigouBackstageService  iAigouBackstageService ;
	 @Action(value ="login",
			 results={@Result(name ="success",location="/WEB-INF/jsp/aigou/admin/index/index.jsp")})
	 public String login(){
		 HttpServletRequest request = ServletActionContext.getRequest();
		 String username = request.getParameter("username");
		 String password = request.getParameter("password");
		 
		 BackstageUser backstageUser=iAigouBackstageService.getUserByUsername(username);
		 if (backstageUser==null) {
				request.setAttribute("errMsg","账号不存在");
				return ERROR;
			}
			if (!backstageUser.getPassword().equals(password)) {
				request.setAttribute("errMsg","账号或密码错误");
				return INPUT;
			}
			reference._adminuUername=backstageUser.getUsername();
			request.setAttribute("_admin",backstageUser.getUsername());
			
		 return SUCCESS;
	 }
	 
	 /**
		 * 退出登录
		 * @param request
		 * @param response
		 * @throws IOException 
		 */
	  @Action(value ="/admin/logout",
			  results={@Result(name ="success",location="/WEB-INF/jsp/aigou/admin/login/login.jsp")})
		public String logout() {
			 HttpServletRequest request = ServletActionContext.getRequest();
			  System.out.println("退出登录");
			
			return SUCCESS;
		}

}
